package org.skipper.samples.redission.key;

import org.redisson.api.RedissonClient;
import org.skipper.samples.redission.enums.RedisKeyEnum;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author shoujian
 * Date: 2021-08-12
 * Time: 下午7:39
 */
@Component
public class KeySource {

    @Qualifier(value = "redis-cloud_0")
    @Resource
    private RedissonClient redissonClient;

    public void decr() {
        long decr_decrementAndGet = redissonClient.getAtomicLong(RedisKeyEnum.KEY_TEST.getCode()).decrementAndGet();
        System.out.println("decr_decrementAndGet = " + decr_decrementAndGet);

        long decr_getAndDecrement = redissonClient.getAtomicLong(RedisKeyEnum.KEY_TEST.getCode()).getAndDecrement();
        System.out.println("decr_getAndDecrement = " + decr_getAndDecrement);
    }

    public void del() {
        redissonClient.getBucket(RedisKeyEnum.KEY_TEST.getCode()).delete();
        String del_getAndDelete = (String) redissonClient.getBucket(RedisKeyEnum.KEY_TEST.getCode()).getAndDelete();
        System.out.println("del_getAndDelete = " + del_getAndDelete);
    }

    public void persist() {
        redissonClient.getPermitExpirableSemaphore(RedisKeyEnum.KEY_TEST.getCode()).clearExpire();
    }

    public void pexipire() {
        redissonClient.getPermitExpirableSemaphore(RedisKeyEnum.KEY_TEST.getCode()).expire(1, TimeUnit.DAYS);
    }

    public void pexpireat() {
        redissonClient.getPermitExpirableSemaphore(RedisKeyEnum.KEY_TEST.getCode()).expireAt(new Date(System.currentTimeMillis() + 1000 * 3600 * 24));
    }

}
